import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../components/Login.vue'
const Login = ()=>import(/*webpackChunkName: "Login_home_welcome" */"../components/Login.vue")
// import Home from '../components/Home.vue'
const Home = ()=>import(/*webpackChunkName: "Login_home_welcome" */"../components/Home.vue")
// import Welcome from '../components/Welcome.vue'
const Welcome= ()=>import(/*webpackChunkName: "Login_home_welcome" */"../components/Welcome.vue")
// import Users from '../components/User/Users.vue'
const Users= ()=>import(/*webpackChunkName: "Users_rights_roles" */"../components/User/Users.vue")
// import Rights from '../components/power/Rights.vue'
const Rights= ()=>import(/*webpackChunkName: "Users_rights_roles" */"../components/power/Rights.vue")
// import Roles from '../components/power/Roles.vue'
const Roles= ()=>import(/*webpackChunkName: "Users_rights_roles" */"../components/power/Roles.vue")
// import Cate from '../components/goods/Cate.vue'
const  Cate = ()=>import(/*webpackChunkName: "Cate_params" */"../components/goods/Cate.vue")
// import Params from '../components/goods/Params.vue'
const Params= ()=>import(/*webpackChunkName: "Cate_params" */"../components/goods/Params.vue")
// import Goods from '../components/goods/Goods.vue'
const Goods= ()=>import(/*webpackChunkName: "Goods_add" */"../components/goods/Goods.vue")
// import Add from '../components/goods/Add.vue'
const Add= ()=>import(/*webpackChunkName: "Goods_add" */"../components/goods/Add.vue")
// import Orders from '../components/order/Orders.vue'
const Orders= ()=>import(/*webpackChunkName: "Orders_reports" */"../components/order/Orders.vue")
// import Reports from '../components/report/Reports.vue'
const Reports = ()=>import(/*webpackChunkName: "Orders_reports" */"../components/report/Reports.vue")

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },{
    path: '/home',
    name: 'home',
    component: Home,
    redirect:'/welcome',
    children:[
      {
        path:'/welcome',
        name:'welcome',
        component:Welcome,
      },
      {
        path:'/users',
        name:'users',
        component:Users,
      },
      {
        path:'/rights',
        name:'rights',
        component:Rights,
      },
      {
        path:'/roles',
        name:'roles',
        component:Roles,
      },
      {
        path:'/categories',
        name:'cate',
        component:Cate,
      },
      {
        path:'/params',
        name:'params',
        component:Params,
      },
      {
        path:'/goods',
        name:'goods',
        component:Goods,
      },
      {
        path:'/goods/add',
        name:'add',
        component:Add,
      },
      {
        path:'/orders',
        name:'orders',
        component:Orders,
      },
      {
        path:'/reports',
        name:'reports',
        component:Reports,
      },
    ]
  }
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const router = new VueRouter({
  routes
})
//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  //to将要访问的路径
  //from 代表从哪个路径跳转而来
  //next 是一个函数
  //next() 放行 next('/login')强制跳转

  if(to.path==='/login') return next();
  //获取token
  const tokenStr= window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/login')
  next()
  
})

export default router
